package com.example.studyapp

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.IBinder
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.google.gson.Gson
import org.json.JSONObject
import java.lang.reflect.Method

data class layout_struct_info(
    var layer: Int,
    var layer_array: Array<Int>
)

//负责显示主要布局
class study_sub : AppCompatActivity() {
    lateinit var view: LinearLayout
    //设置界面
    lateinit var view_setting:View
    lateinit var view_paper:View
    lateinit var change_uername:View
    lateinit var change_password:View


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_study_sub)
        //如果当前没有设置布局，就停止加载
        if(define_study_sub_arg1==layout_class.LAYOUT_CLASS_NULL) {
            return
        }
        view=this.findViewById(R.id.sub_view)
        view_setting=LayoutInflater.from(this).inflate(R.layout.activity_study_setting, null)
        //试卷页面
        view_paper=LayoutInflater.from(this).inflate(R.layout.study_paper_info, null)
        change_uername=LayoutInflater.from(this).inflate(R.layout.activity_study_setting, null)
        change_password=LayoutInflater.from(this).inflate(R.layout.activity_study_setting, null)


        //监听标题栏返回按钮
        val ret:Button=this.findViewById(R.id.sub_ret)
        ret.setOnClickListener { v->
            this.finish()
        }
        //切换要显示的布局
        if(define_study_sub_arg1==layout_class.LAYOUT_CLASS_SETTING){
            //设置窗口
            study_sub_setting(view)
        }else if(define_study_sub_arg1==layout_class.LAYOUT_CLASS_QU){
            study_sub_paper()
        }

    }

    fun study_sub_setting( myview: View) {
        //加载设置界面
        val setview =
            LayoutInflater.from(view.context).inflate(R.layout.activity_study_setting, null)
        view.removeAllViews()
        view.addView(setview, view.layoutParams)

        //处理设置界面
        val setting_change_icon: LinearLayout = myview.findViewById(R.id.setting_change_icon)
        val setting_change_username: LinearLayout =
            myview.findViewById(R.id.setting_change_username)
        val setting_change_password: LinearLayout =
            myview.findViewById(R.id.setting_change_password)
        val setting_logout: Button = myview.findViewById(R.id.myinfo_logout)
        setting_change_username.setOnClickListener { v ->
            Toast.makeText(v.context, "修改昵称", Toast.LENGTH_SHORT)
                .show()
            //切换
            val cuinet = Intent(this, change_uername_1::class.java)
            startActivity(cuinet)
        }
        setting_change_password.setOnClickListener { v ->
            Toast.makeText(v.context, "修改密码", Toast.LENGTH_SHORT)
                .show()
            val cpinet = Intent(this, change_password_1::class.java)
            startActivity(cpinet)
        }
        setting_logout.setOnClickListener { v ->
            Toast.makeText(v.context, "退出", Toast.LENGTH_SHORT)
                .show()
            logout_goto()
        }
    }
    //登出按钮
    fun logout(){
        logout_goto()
    }
    //登出到登录界面（通过重启应用实现）
    fun logout_goto(){
        //收到消息，如果允许退出
        val serviceManager = Class.forName("android.os.ServiceManager")
        val getService: Method = serviceManager.getMethod("getService", String::class.java)
        val remoteService: Any = getService.invoke(null, AppCompatActivity.POWER_SERVICE)
        val stub = Class.forName("android.os.IPowerManager\$Stub")
        val asInterface: Method = stub.getMethod("asInterface", IBinder::class.java)
        val powerManager: Any = asInterface.invoke(null, remoteService)
        val shutdown: Method = powerManager.javaClass.getDeclaredMethod(
            "reboot",
            Boolean::class.javaPrimitiveType,
            String::class.java,
            Boolean::class.javaPrimitiveType
        )
        shutdown.invoke(powerManager, false, "", true)
    }

    //加载试卷信息
    fun study_sub_paper(){
        //初始化试卷列表
        val pview =
            LayoutInflater.from(view.context).inflate(R.layout.study_paper_info, null)
        view.removeAllViews()
        view.addView(pview, view.layoutParams)

        try{//尝试获取试卷信息
            val sendpaper:JSONObject= JSONObject(Gson().toJson(jstruct_get_paper(papername = define_study_sub_title)))
            object :jsonInteraction(sendpaper){
                //收到试题信息，设置主界面
                override fun myResposeListener(response: JSONObject) {
                    set_paper(response,pview)
                    //Toast.makeText(this@study_sub,,Toast.LENGTH_LONG)
                       // .show()
                }
            }
        }catch (e: java.lang.Exception){

        }
    }
    //设置试卷界面
    @SuppressLint("SetTextI18n")
    fun set_paper(response: JSONObject, myview:View):String{
        val jspi:jstruct_paper_info=Gson().fromJson(response.toString(),jstruct_paper_info::class.java)
        if(jspi.error){
            return "获取数据失败"
        }
        val view_title:TextView = this.findViewById(R.id.sub_title)
        val paper_info_num:TextView=myview.findViewById(R.id.paper_info_num)
        val paper_info_num_cho:TextView=myview.findViewById(R.id.paper_info_num_cho)
        val paper_info_num_com:TextView=myview.findViewById(R.id.paper_info_num_com)
        val paper_info_start:Button=myview.findViewById(R.id.paper_info_start)
        val paper_info_return:Button=myview.findViewById(R.id.paper_info_return)
        var nums:Int=jspi.choice_qu_num+jspi.completion_qu_num
        view_title.text="网络体系结构"
        /*
        paper_info_num.text=define_study_paper_info_nums+nums.toString()

        paper_info_num_cho.text=define_study_paper_info_cho_nums+jspi.choice_qu_num.toString()
        paper_info_num_com.text=define_study_paper_info_com_nums+jspi.completion_qu_num.toString()
        */
        paper_info_num.text=define_study_paper_info_nums+"50"

        paper_info_num_cho.text=define_study_paper_info_cho_nums+"30"
        paper_info_num_com.text=define_study_paper_info_com_nums+"20"
        //监听当前界面
        paper_info_start.setOnClickListener {
            //切换到做题界面,首先设置公共变量，保存试题信息
            define_now_paper_info=jspi
            val inte=Intent(this,study_choice_question_1::class.java)
            startActivity(inte)
        }
        paper_info_return.setOnClickListener {
            this.finish()
        }
        return "读取成功"
    }

}

